home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / di_win.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  9.1 KB  |  388 lines

  1. /*
  2.  * The dialing window routines.
  3.  */
  4.  
  5. #define MAX_PASS    25
  6.  
  7. #include <stdio.h>
  8. #include <curses.h>
  9. #include "config.h"
  10. #include "dial_dir.h"
  11. #include "misc.h"
  12. #include "modem.h"
  13. #include "param.h"
  14.  
  15. /*
  16.  * The dialing window.  Its job is to kill the input routine, get a port,
  17.  * cycle thru the entries in the queue, while interpreting both the
  18.  * user's requests and the modem's responses.  A non-zero return code
  19.  * means we're ready to fire up the input routine.
  20.  */
  21.  
  22. int
  23. dial_win()
  24. {
  25.     extern int rc_index, fd;
  26.     WINDOW *di_win, *newwin();
  27.     int i, j, key, want_out, pass, tic, baud;
  28.     static int can_sync();
  29.     long now, time();
  30.     char *tbuf, *ctime(), *str, cr=13, *read_codes();
  31.     void dial_it(), delay_times(), input_off();
  32.     void error_win(), line_set(), hang_up(), zap_vs(), log_calls();
  33.     static void disp_queue();
  34.     unsigned int sleep();
  35.                     /* are we already talking? */
  36.     input_off();
  37.     hang_up(VERBOSE);
  38.  
  39.     touchwin(stdscr);
  40.     refresh();
  41.  
  42.     /*
  43.      * If the phone number points to NULL, then either you're on a
  44.      * direct line, or you want to do the dialing yourself.
  45.      */
  46.     if (*dir->number[dir->q_num[0]] == '\0') {
  47.                     /* check LD permission */
  48.         if (limit_ld(0))
  49.             return(0);
  50.  
  51.         if (get_port())
  52.             return(0);
  53.                     /* can't talk directly to OBM */
  54.         if (!strcmp(modem->mname[modem->m_cur], "OBM")) {
  55.             error_win(0, "Can't access the On Board Modem directly",
  56.              "You must use the automatic dialing feature");
  57.             return(0);
  58.         }
  59.  
  60.         zap_vs();
  61.         touchwin(stdscr);
  62.         clear();
  63.         printw("Connected to /dev/%s at %d baud...\n", modem->tty[modem->t_cur], dir->baud[dir->d_cur]);
  64.         refresh();
  65.         return(1);
  66.     }
  67.  
  68.     di_win = newwin(17, 70, 3, 5);
  69.                     /* the basic window */
  70.     mvwattrstr(di_win, 1, 20, A_BOLD, "D I A L I N G       W I N D O W");
  71.     horizontal(di_win, 2, 0, 70);
  72.     mvwaddstr(di_win, 4, 23, "System name:");
  73.     mvwaddstr(di_win, 5, 23, "Pass number:");
  74.     mvwaddstr(di_win, 6, 14, "Elapse time this try:");
  75.     mvwaddstr(di_win, 7, 13, "Time at start of dial:");
  76.     mvwaddstr(di_win, 8, 9, "Time at start of this try:");
  77.     mvwaddstr(di_win, 9, 16, "Connect delay time:");
  78.     mvwaddstr(di_win, 10, 17, "Redial delay time:");
  79.     mvwaddstr(di_win, 11, 24, "Script/TTY:");
  80.     mvwaddstr(di_win, 12, 16, "Result of last try:");
  81.  
  82.     mvwaddstr(di_win, 14, 3, "<SPACE>: Recycle");
  83.     mvwaddstr(di_win, 14, 22, "<DEL>: Remove from queue");
  84.     mvwaddstr(di_win, 14, 49, "E: Change delays");
  85.  
  86.                     /* the start time */
  87.     time(&now);
  88.     tbuf = ctime(&now);
  89.     tbuf[19] = '\0';
  90.     mvwaddstr(di_win, 7, 36, &tbuf[11]);
  91.  
  92.     mvwprintw(di_win, 9, 36, "%-4d", param->c_delay);
  93.     mvwprintw(di_win, 10, 36, "%-4d", param->r_delay);
  94.  
  95.     box(di_win, VERT, HORZ);
  96.     mvwaddstr(di_win, 16, 24, " Press <ESC> to abort ");
  97.  
  98.     pass = 0;
  99.     i = 0;
  100.     want_out = 0;
  101.     while (!want_out && pass <= MAX_PASS) {
  102.         key = -1;
  103.         pass++;
  104.                     /* update the d_cur variable */
  105.         dir->d_cur = dir->q_num[i];
  106.                     /* check LD permission */
  107.         if (limit_ld(i)) {
  108.             want_out++;
  109.             break;
  110.         }
  111.                     /* get a port */
  112.         if (get_port()) {
  113.             want_out++;
  114.             break;
  115.         }
  116.                     /* fill in the window */
  117.         disp_queue(di_win, dir->d_cur, pass);
  118.  
  119.         /*
  120.          * The actual dial routine.  The "i" is the index into the
  121.          * queue, not the entry number.  Returns immediately without
  122.          * waiting for a carrier.
  123.          */
  124.         dial_it(i);
  125.         tty_flush(fd, 0);
  126.  
  127.         /*
  128.          * Here we do a time-slice between reading the result codes
  129.          * from the modem and reading the keyboard.  The one second
  130.          * granularity won't be too accurate, but who cares?
  131.          */
  132.         tic = 0;
  133.         rc_index = 0;
  134.         while (tic < param->c_delay) {
  135.             if ((str = read_codes()) == NULL) {
  136.                 mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  137.                 wrefresh(di_win);
  138.             }
  139.             else {
  140.                 /*
  141.                  * A return code that converts to an number
  142.                  * that is less than 300 is probably an error
  143.                  * message.
  144.                  */
  145.                 baud = atoi(str);
  146.                 if (baud < 300) {
  147.                     mvwprintw(di_win, 12, 36, "%-20.20s", str);
  148.                     wmove(di_win, 12, 36);
  149.                     wrefresh(di_win);
  150.                     break;
  151.                 }
  152.                     /* we're connected */
  153.                 beep();
  154.                 clear_line(di_win, 12, 36, TRUE);
  155.                 wattrstr(di_win, A_BLINK, "CONNECTED");
  156.                 wmove(di_win, 12, 36);
  157.                 wrefresh(di_win);
  158.                 wait_key(di_win, 2);
  159.                 delwin(di_win);
  160.  
  161.                 /*
  162.                  * Did the modem sync at a different baud
  163.                  * rate than what we expected?
  164.                  */
  165.                 if (dir->baud[dir->d_cur] != baud) {
  166.                     if (can_sync(baud)) {
  167.                         dir->baud[dir->d_cur] = baud;
  168.                         line_set();
  169.                     }
  170.                 }
  171.  
  172.                 zap_vs();
  173.                 touchwin(stdscr);
  174.                 clear();
  175.                 printw("Connected to %s at %d baud...\n",
  176.                  dir->name[dir->d_cur], dir->baud[dir->d_cur]);
  177.                 refresh();
  178.  
  179.                     /* log the call */
  180.                 log_calls(i);
  181.                 return(1);
  182.             }
  183.             if (tic == param->c_delay)
  184.                 break;
  185.                     /* ok... try the keyboard */
  186.             if ((key = wait_key(di_win, 1)) != -1)
  187.                 break;
  188.  
  189.             mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  190.             wrefresh(di_win);
  191.         }
  192.         /*
  193.          * If the modem did not return a code, then we need to
  194.          * stop it.  Sending a CR will stop most modems cold,
  195.          * except of course for the OBM...
  196.          */
  197.         if (str == NULL) {
  198.             if (!strcmp(modem->mname[modem->m_cur], "OBM"))
  199.                 hang_up(QUIET);
  200.             else
  201.                 write(fd, &cr, 1);
  202.             sleep(2);
  203.         }
  204.                     /* if we get here, no key was pressed */
  205.         if (key == -1) {
  206.             clear_line(di_win, 6, 14, TRUE);
  207.             mvwaddstr(di_win, 6, 27, "Pausing:");
  208.                     /* no return code? */
  209.             if (str == NULL) {
  210.                 clear_line(di_win, 12, 36, TRUE);
  211.                 waddstr(di_win, "TIMED OUT");
  212.                 wmove(di_win, 12, 36);
  213.             }
  214.                     /* do the pause */
  215.             tic = 0;
  216.             while (tic < param->r_delay) {
  217.                 if ((key = wait_key(di_win, 1)) != -1)
  218.                     break;
  219.                 mvwprintw(di_win, 6, 36, "%-4d", ++tic);
  220.                 wrefresh(di_win);
  221.             }
  222.             clear_line(di_win, 6, 14, TRUE);
  223.             waddstr(di_win, "Elapse time this try:");
  224.         }
  225.         mvwaddstr(di_win, 6, 36, "0   ");
  226.                     /* Process the keystroke */
  227.         switch (key) {
  228.             case ' ':    /* next in the queue */
  229.                 clear_line(di_win, 12, 36, TRUE);
  230.                 waddstr(di_win, "RECYCLED");
  231.                 wmove(di_win, 12, 36);
  232.                 wrefresh(di_win);
  233.                 /* fall thru... */
  234.             case -1:    /* no key was pressed */
  235.                 i++;
  236.                 if (i > NUM_QUEUE)
  237.                     i = 0;
  238.                 if (dir->q_num[i] == -1)
  239.                     i = 0;
  240.                 break;
  241.             case DEL:    /* <DEL> key, remove from queue */
  242.                 if (dir->q_num[1] == -1) {
  243.                     beep();
  244.                     clear_line(di_win, 12, 36, TRUE);
  245.                     waddstr(di_win, "NO MORE ENTRIES");
  246.                     wmove(di_win, 12, 36);
  247.                     wrefresh(di_win);
  248.                     wait_key(di_win, 3);
  249.                     break;
  250.                 }
  251.                 clear_line(di_win, 12, 36, TRUE);
  252.                 waddstr(di_win, "ENTRY DELETED");
  253.                 wmove(di_win, 12, 36);
  254.                 wrefresh(di_win);
  255.                 wait_key(di_win, 3);
  256.  
  257.                     /* compact the queue */
  258.                 for (j=i; j<NUM_QUEUE-1; j++)
  259.                     dir->q_num[j] = dir->q_num[j+1];
  260.                 dir->q_num[NUM_QUEUE-1] = -1;
  261.  
  262.                 if (dir->q_num[i] == -1)
  263.                     i = 0;
  264.                 break;
  265.             case 'e':
  266.             case 'E':    /* change delay time */
  267.                 delay_times();
  268.                 touchwin(di_win);
  269.                 mvwprintw(di_win, 9, 36, "%-4d", param->c_delay);
  270.                 mvwprintw(di_win, 10, 36, "%-4d", param->r_delay);
  271.                 break;
  272.             case ESC:    /* <ESC> key */
  273.                 beep();
  274.                 clear_line(di_win, 12, 36, TRUE);
  275.                 wattrstr(di_win, A_BLINK, "DIAL ABORTED");
  276.                 wmove(di_win, 12, 36);
  277.                 wrefresh(di_win);
  278.                 wait_key(di_win, 3);
  279.                 want_out++;
  280.                 break;
  281.             default:
  282.                 beep();
  283.                 break;
  284.         }
  285.     }
  286.                     /* clean up and go home */
  287.     werase(di_win);
  288.     wrefresh(di_win);
  289.     delwin(di_win);
  290.     if (!want_out)
  291.         error_win(0, "Exceeded the maximum number number of dialing attempts", "");
  292.     return(0);
  293. }
  294.  
  295. /*
  296.  * Display what info we know at this time.
  297.  */
  298.  
  299. static void
  300. disp_queue(win, entry, pass)
  301. WINDOW *win;
  302. int entry, pass;
  303. {
  304.     long now, time();
  305.     char *tbuf, *ctime();
  306.     void st_line();
  307.                     /* redo the status line */
  308.     st_line("");
  309.                     /* system name */
  310.     clear_line(win, 4, 36, TRUE);
  311.     waddstr(win, dir->name[entry]);
  312.                     /* pass number */
  313.     mvwprintw(win, 5, 36, "%-4d", pass);
  314.                     /* time of this call */
  315.     time(&now);
  316.     tbuf = ctime(&now);
  317.     tbuf[19] = '\0';
  318.     mvwaddstr(win, 8, 36, &tbuf[11]);
  319.                     /* the script field */
  320.     clear_line(win, 11, 36, TRUE);
  321.     waddstr(win, dir->script[entry]);
  322.  
  323.     wmove(win, 12, 36);
  324.     wrefresh(win);
  325.     return;
  326. }
  327.  
  328. /*
  329.  * Determine if the modem can detect the synchronization of the connected
  330.  * baud rate.  We check the modem database and see if the connect string
  331.  * is unique.  A non-zero return code means the modem can sync.
  332.  */
  333.  
  334. static int
  335. can_sync(baud)
  336. int baud;
  337. {
  338.     int i;
  339.     char *str;
  340.                     /* feature disabled? */
  341.     if (modem->auto_baud[modem->m_cur] != 'Y')
  342.         return(0);
  343.                     /* re-construct the string */
  344.     switch (baud) {
  345.         case 300:
  346.             str = modem->con_3[modem->m_cur];
  347.             break;
  348.         case 1200:
  349.             str = modem->con_12[modem->m_cur];
  350.             break;
  351.         case 2400:
  352.             str = modem->con_24[modem->m_cur];
  353.             break;
  354.         case 4800:
  355.             str = modem->con_48[modem->m_cur];
  356.             break;
  357.         case 9600:
  358.             str = modem->con_96[modem->m_cur];
  359.             break;
  360.         case 19200:
  361.             str = modem->con_192[modem->m_cur];
  362.             break;
  363.         default:
  364.             return(0);
  365.     }
  366.  
  367.     if (*str == '\0')
  368.         return(0);
  369.                     /* test "str" against all others */
  370.     i = 0;
  371.     if (!strcmp(str, modem->con_3[modem->m_cur]))
  372.         i++;
  373.     if (!strcmp(str, modem->con_12[modem->m_cur]))
  374.         i++;
  375.     if (!strcmp(str, modem->con_24[modem->m_cur]))
  376.         i++;
  377.     if (!strcmp(str, modem->con_48[modem->m_cur]))
  378.         i++;
  379.     if (!strcmp(str, modem->con_96[modem->m_cur]))
  380.         i++;
  381.     if (!strcmp(str, modem->con_192[modem->m_cur]))
  382.         i++;
  383.                     /* should match only itself */
  384.     if (i == 1)
  385.         return(1);
  386.     return(0);
  387. }
  388.